--[[
	Exsto
	Copyright (C) 2010  Prefanatic

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

--[[ -----------------------------------
	Category:  Script Loading/Resources
    ----------------------------------- ]]

	exstoInclude( "exsto/gmodfunc_port.lua" )
	exstoInclude( "exsto/json.lua" )
	exstoInclude( "exsto/sh_tables.lua" )
	exstoInclude( "exsto/sh_print.lua" )
	exstoInclude( "exsto/fel.lua" )
	exstoInclude( "exsto/sv_variables.lua" )
	exstoInclude( "exsto/sv_commands.lua" )
	--exstoInclude( "exsto/sh_access.lua" )
	--exstoInclude( "exsto/sv_access.lua" )
	--exstoInclude( "exsto/sh_plugins.lua" )

--[[ -----------------------------------
	Category:  Player Utils
     ----------------------------------- ]]
--[[local ply = _R.Player
local nick = ply.Nick

function ply:IsConsole() if !self:IsValid() then return false end end 
function ply:Nick() if !ValidEntity( self ) then return "UNKNOWN" else return nick( self ) end end
	ply.Name = ply.Nick
]]

--[[ -----------------------------------
	Category:  Console Utils
     ----------------------------------- ]]
--[[
local console = _R.Entity

function console:Name() if !self:IsValid() then return "Console" end end
function console:Nick() if !self:IsValid() then return "Console" end end
function console:IsAllowed() if !self:IsValid() then return true end end
function console:IsSuperAdmin() if !self:IsValid() then return true end end
function console:IsAdmin() if !self:IsValid() then return true end end
function console:IsConsole() if !self:IsValid() then return true end end
function console:IsPlayer() if !self:IsValid() then return false end end
]]
--[[ -----------------------------------
	Category:  Player Extras
     ----------------------------------- ]]
function exsto.BuildPlayerNicks()
	local tbl = {}
	
	for k,v in ipairs( player.GetAll() ) do
		table.insert( tbl, v:Nick() )
	end
	return tbl
end

function exsto.FindPlayer( ply )
	return exsto.FindPlayers( ply )[1] or nil
end

function exsto.FindPlayers( search, ply )
	local players = {}
	local inverse = 0
	
	if search:sub(1,1) == "!" then
		inverse = 1
		search = search:Replace("!","")
	end
	
	if search == "me" then
		table.insert(players, ply)
	
	-- Rank styles
	elseif search:sub( 1, 1 ) == "%" then
		search = search:Replace( "%", "" ):lower()
		for _, ply in ipairs( player.GetAll() ) do
			if ply:GetRank() == search then table.insert( players, ply ) end
		end
	else -- Other stuff
		
		-- Are we wildcarding?
		local wc, start = search:find( "*", 1, true )
		if wc and start and ( start == search:len() ) and ( not search:sub( start - 1 ) == " " ) then
			search = search:gsub( "*", "" ):lower()
			for _, ply in ipairs( player.GetAll() ) do
				if ply:Nick():lower():find( search, 1, true ) then table.insert( players, ply ) end
			end
		
		-- We are just specific matching.
		else
			for _, ply in ipairs( player.GetAll() ) do
				if ply:Nick():lower():find( search:lower(), 1, true ) then table.insert( players, ply ) break end
			end
		end
	end
	
	if inverse == 1 then
		allPlayers = player.GetAll()
		newPlayers = {}
		for _,ply in pairs(allPlayers) do
			if not table.HasValue(players,ply) then
				table.insert(newPlayers,ply)
			end
		end
		players = newPlayers
	end
	
	return players
end

-- Init some items.
	--exsto.LoadPlugins()
	--exsto.InitPlugins()

	--exsto.LoadFlags()
	--exsto.CreateFlagIndex()
	
	-- After everything is done; update the owner with his flags :)
	--if exsto.Ranks[ "srv_owner" ] then
		--exsto.Ranks[ "srv_owner" ].AllFlags = exsto.FlagIndex
	--end
	
	--local seconds = SysTime() - exsto.StartTime
	print( "----------------------------------------------" )
	print( "Exsto started in god knows how many seconds!" )
	print( "----------------------------------------------" )